import 'package:flutter/material.dart';
import 'package:redux/redux.dart';
import 'package:stock_check/widget/CommonsWidget.dart';
import 'package:stock_check/widget/MyDrawer.dart';

class MyScaffold extends StatefulWidget {
  final Widget child;
  final Widget title;
  final Store store;
  final bool noLeading;
  final Widget bottom;

  MyScaffold({
    @required this.title,
    @required this.child,
    @required this.store,
    this.bottom,
    this.noLeading = false
  }):assert(child!=null&&child!=null&&store!=null);
  @override
  _MyScaffoldState createState() => _MyScaffoldState();
}

class _MyScaffoldState extends State<MyScaffold> with SingleTickerProviderStateMixin {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  AnimationController _controller;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(
        vsync: this,
        value: 1.0,
        duration: Duration(milliseconds: 300)
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  // 监听Drawer开合
  void listenDrawer(isOpen){
    _controller.fling(velocity: isOpen ? -1.0 : 1.0);
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        key: _scaffoldKey,
        drawer:MyDrawer(listenDrawer),
        appBar: AppBar(
          leading: !widget.noLeading?Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: CommonsWidget.getIconsByNetWork(widget.store.state.appInfo.netWork),
          ):BackButton(),
          title: widget.title,
          actions: <Widget>[
            IconButton(
              onPressed: _toggleFrontLayer,
              icon: AnimatedIcon(
                  icon: AnimatedIcons.close_menu,
                  color: Colors.white,
                  progress: _controller,
              ),
            )
          ],
          bottom: widget.bottom,
          centerTitle: true,
          elevation: 2.0,
        ),
        body: widget.child,
    );
  }

  // 切换抽屉
  void _toggleFrontLayer() {
    final AnimationStatus status = _controller.status;
    final bool isOpen = status == AnimationStatus.completed || status == AnimationStatus.forward;
    _controller.fling(velocity: isOpen ? -1.0 : 1.0);
    if(isOpen){
      _scaffoldKey.currentState.openDrawer();
    }
  }
}